﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using Estimate.Common.DBFieldTypes;
using Estimate.Common.Repository;
using Estimate.Common.Entity;
using Estimate.Common.Estimate;
using Estimate.Common.Estimate.EstimateValue;
using Newtonsoft.Json;

namespace Estimate.Reports
{
    /// <summary>
    /// 省台预报员成绩报表类
    /// </summary>
    public class STIndividualReport
    {
        #region 变量与对象
        private FullInfoReport memberReportScore;// 得分对象
        private FullInfoReport standardReportScore;// 标准评分对象
        protected string _connectionString;
        ExecutionStatus exeStatus;//程序执行状态
        IndividualReportResult Result = new IndividualReportResult();//省台预报员详细成绩结果

        #endregion

        #region 构造函数
        
        /// <summary>
        /// 省台预报员成绩报表类构造函数
        /// </summary>
        /// <param name="forecaster">省台预报员</param>
        /// <param name="connectionString"></param>
        /// <param name="tid"></param>
        public STIndividualReport(string forecaster, string connectionString, string tid)
        {
            _connectionString = connectionString;

            if (tid != "") exeStatus = new ExecutionStatus(tid);
            if (exeStatus != null) exeStatus.SetMessage("Initial", "正在准备...");

            forecaster = forecaster.Replace("'", "");

            memberReportScore = new FullInfoReport(_connectionString, ForeRepositoryFactory.SiteForeRepository(_connectionString, forecaster));
            memberReportScore.CulculateAllResult = true;
            memberReportScore.CulculateDeviationResult = false;
            memberReportScore.ExeStatus = exeStatus;

            standardReportScore = new FullInfoReport(_connectionString, ForeRepositoryFactory.ZytCityForeRepository(_connectionString));
            standardReportScore.CulculateAllResult = true;
            standardReportScore.CulculateDeviationResult = false;
            standardReportScore.ExeStatus = exeStatus;
           

            if (exeStatus != null) exeStatus.DeleteMessage("Initial");

        }

        #endregion

        #region  数据准备并计算
        public void PrepareData(DateTime beginDate, DateTime endDate, ForeSendpointType[] sendpoints, ForeSemiType semitype)
        {
            ForeSpanType[] waterSpans = new ForeSpanType[] { ForeSpanType.FD00_12, ForeSpanType.FD12_24, ForeSpanType.FD00_24, ForeSpanType.FD24_48, ForeSpanType.FD48_72 };
            ForeSpanType[] tempSpans = new ForeSpanType[] { ForeSpanType.FD00_24, ForeSpanType.FD24_48, ForeSpanType.FD48_72 };
           
            try
            {
                if (exeStatus != null)
                {
                    exeStatus.TotalSteps = memberReportScore.GetExeStatusTotalSteps(beginDate, endDate, semitype, waterSpans, tempSpans) + standardReportScore.GetExeStatusTotalSteps(beginDate, endDate, semitype, waterSpans, tempSpans);
                    exeStatus.SetMessage("memberReportScore", "计算标准得分");
                }

                memberReportScore.PrepareData(beginDate, endDate, semitype, waterSpans, tempSpans, sendpoints);

                if (exeStatus != null)
                {
                    exeStatus.DeleteMessage("memberReportScore");
                    exeStatus.SetMessage("standardReportScore", "计算技巧值评分标准");
                }


                standardReportScore.PrepareData(beginDate, endDate, semitype, waterSpans, tempSpans, sendpoints);

                if (exeStatus != null) exeStatus.DeleteMessage("standardReportScore");

            }
            catch (Exception ex)
            {

                Result.success = false.ToString();
                Result.msg = ex.ToString();
                if (exeStatus != null)
                {
                    exeStatus.DeleteMessage("memberReportScore");
                    exeStatus.DeleteMessage("standardReportScore");
                    exeStatus.SetMessage("Error", "程序执行出错");
                }
            }

        }

        
        #endregion

        #region  获取省台预报员成绩字符串
        /// <summary>
        /// 获取省台预报员成绩字符串
        /// </summary>
        /// <returns></returns>
        public string GetReportDataString()
        {
            if (exeStatus != null) exeStatus.SetMessage("GetReportDataString", "获取计算结果");

            try
            {
                Result.result["data"] = memberReportScore.ToResult(standardReportScore, "");
            }
            catch (Exception ex)
            {

                Result.success = false.ToString();
                Result.msg = ex.ToString();
                if (exeStatus != null)
                    exeStatus.SetMessage("Error", "程序执行出错");

            }
            if (exeStatus != null)
            {
                exeStatus.DeleteMessage("GetReportDataString");
                exeStatus.SetMessage(true, "ExecutionEnd", "执行完毕");
            }

            return JsonConvert.SerializeObject(Result);
        }
        #endregion
    }

}
